package com.yuandian.business.monitor.controller;

import com.yuandian.api.business_monitor.po.ComponentParams;
import com.yuandian.api.business_monitor.vo.ComponentDetailVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.IComponentService;
import com.yuandian.constant.Constants;
import com.yuandian.utils.YDateUtil;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 网端组件控制器
 *
 * @Author wangpeiyang
 * @Date 2021/5/26 13:54
 */
@RestController
@Api(value = "网端组件接口", tags = "网端组件控制器接口")
@RequestMapping("/component")
public class ComponentController {
    @Autowired
    private IComponentService componentService;

    /**
     * 查看组件总览列表
     *
     * @param params
     * @return
     */
	@ApiOperation("查看组件总览列表")
    @PostMapping("/queryComponentDetailList")
    public R queryComponentDetailList(ComponentParams params) {
        if (params == null) {
            return R.failed("参数不能为空");
        }
        if (params.getSystemId() == null) {
            return R.failed("系统Id不能为空");
        }
        if (params.getSegmentId() == null) {
            return R.failed("网端Id不能为空");
        }
        return R.ok(componentService.queryComponentDetailList(params));
    }


    /**
     * 查看组件总览列表
     *
     * @param params
     * @return
     */
	@ApiOperation("查看组件总览下载")
    @PostMapping("/queryComponentDetailListExport")
    public void queryComponentDetailListExport(@RequestBody ComponentParams params, HttpServletResponse response) throws ParseException, IOException {

        List<ComponentDetailVo> list = new ArrayList<>();
        list = componentService.queryComponentDetailListExport(params);

        list.forEach(componentDetailVo -> {
            if (componentDetailVo.getAvgDealTime()!=null){
                componentDetailVo.setAvgDealTimeStr(YDateUtil.getMinSecond(getDouble(componentDetailVo.getAvgDealTime() / 1000.0)));
            }
            if (componentDetailVo.getServerAvgTranslateTime()!=null){
                componentDetailVo.setServerAvgTranslateTimeStr(YDateUtil.getMinSecond(getDouble(componentDetailVo.getServerAvgTranslateTime() / 1000.0)));
            }
            if (componentDetailVo.getServerAvgResponseTime()!=null){
                componentDetailVo.setServerAvgResponseTimeStr(YDateUtil.getMinSecond(getDouble(componentDetailVo.getServerAvgResponseTime() / 1000.0)));
            }
            if (componentDetailVo.getSuccessRate()!=null){
                componentDetailVo.setSuccessRateStr(componentDetailVo.getSuccessRate()+"%");
            }
            if (componentDetailVo.getHealthRate()!=null){
                componentDetailVo.setHealthRateStr(componentDetailVo.getHealthRate()+"%");
            }

        });

        String title = "组件详情列表";
        String fileName = title + System.currentTimeMillis();

        String formTitleName = title;

//        前端传systemName和segmentName，若有值，则拼装系统名称和网端名称到表单标题
        if (params.getSystemName() != null && !("").equals(params.getSystemName())) {
            if (params.getSegmentName() != null && !("").equals(params.getSegmentName())) {
                formTitleName = formTitleName + "-" + params.getSystemName() + "系统-" + params.getSegmentName() + "网端";
            } else {
                formTitleName = formTitleName + "-" + params.getSystemName() + "系统";
            }
        }

        formTitleName = formTitleName + Constants.DOUBLE_SPACE + params.getStartTime() + "~" + params.getEndTime();


                List<List<String>> headList = new ArrayList<>();
        List<String> titleList = Arrays.asList("网端名称","组件IP端口", "请求量", "平均响应时长", "平均处理耗时", "平均传输耗时", "告警请求量",
                "失败请求量", "成功率", "健康度");
        String finalFormTitleName = formTitleName;
        titleList.forEach(s -> {
            List<String> childHeadList = new ArrayList<>();
            childHeadList.add(finalFormTitleName);
            childHeadList.add(s);
            headList.add(childHeadList);
        });
        ExcelUtil.writeExcel(ComponentDetailVo.class, fileName, "组件详情列表", list, headList, response);
    }


    private static Double getDouble(Double allTime) {
        if (allTime == null) {
            return 0d;
        }
        return new BigDecimal(allTime).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
